using System;
using System.Collections.ObjectModel;

namespace BigPondUsage
{
    /// <summary>
    /// Utility class. Contains useful functions that are often used.
    /// </summary>
    internal class Utils
    {
        private Utils() { }

        /// <summary>
        /// Turns an unescaped string into an escaped string suitable for an URL.
        /// </summary>
        /// <param name="str">The string to escape.</param>
        /// <returns>The escaped string.</returns>
        public static string escape(string str)
        {
            if (str != null)
            {
                str = str.Replace("%", "%25");
                str = str.Replace(":", "%3A");
                str = str.Replace("/", "%2F");
                str = str.Replace("?", "%3F");
                str = str.Replace("=", "%3D");
                str = str.Replace("&", "%26");
                str = str.Replace("@", "%40");
            }
            
            return str;
        }

        /// <summary>
        /// Searches a given string for the first occurrence of the start marker and returns a substring that contains text after the start marker and before the end marker.
        /// </summary>
        /// <param name="text">The text to be searched.</param>
        /// <param name="start">The start marker to begin the substring.</param>
        /// <param name="end">The end marker to end the substring.</param>
        /// <returns>The first occurrence substring of the text given. A blank string if not found.</returns>
        public static string substring(string text, string start, string end)
        {
            return substringArray(text, start, end)[0];
        }

        /// <summary>
        /// Searches a given string for all occurrences of start marker to end marker and returns a substring collection that contains text after the start marker and before the end marker.
        /// </summary>
        /// <param name="text">The text to be searched.</param>
        /// <param name="start">The start marker to begin the substring.</param>
        /// <param name="end">The end marker to end the substring.</param>
        /// <returns>A string collection containing found substrings in the searched text. A string collection with one blank string if not found.</returns>
        public static Collection<string> substringArray(string text, string start, string end)
        {
            int startLength = start.Length;
            int endLength = end.Length;
            Collection<string> results = new Collection<string>();
            int startPosition = 0;
            int endPosition = 0;

            try
            {
                if (String.IsNullOrEmpty(end))
                {
                    results.Add(text.Substring(text.IndexOf(start, StringComparison.CurrentCulture) + startLength, text.Length - (text.IndexOf(start, StringComparison.CurrentCulture) + startLength)));
                }
                else if (String.IsNullOrEmpty(start))
                {
                    results.Add(text.Substring(0, text.IndexOf(end, StringComparison.CurrentCulture)));
                }
                else
                {
                    while ((startPosition = text.IndexOf(start, startPosition, StringComparison.CurrentCulture)) != -1)
                    {
                        startPosition += startLength;
                        if ((endPosition = text.IndexOf(end, startPosition, StringComparison.CurrentCulture)) != -1)
                        {
                            results.Add(text.Substring(startPosition, (endPosition - startPosition)));
                            startPosition = endPosition + endLength;
                        }
                    }
                }
            }
            catch (ArgumentOutOfRangeException) { }
            catch (NullReferenceException) { }
            finally
            {
                if (results.Count <= 0)
                    results.Add("");
            }

            return results;
        }
    }
}
